package com.hx.vending.service.impl;

import com.hx.vending.bean.Field;
import com.hx.vending.bean.entity.FieldVo;
import com.hx.vending.dao.*;
import com.hx.vending.service.IFieldService;
import com.hx.vending.util.BigDecimalUtils;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FieldServiceImpl implements IFieldService {

    @Autowired
    private FieldMapper fieldMapper;
    @Autowired
    private OnlineOrderMapper onlineOrderMapper;
    @Autowired
    private GoodsConsumeMapper goodsConsumeMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private OrderBuyMapper orderBuyMapper;

    /*
     * 根据id查询场地信息
     * */
    @Override
    public Field selectByPrimary(Field field){
        return fieldMapper.selectByPrimary(field);
    }

    @Override
    public List<Field> selectByExample(Field field) {
        return fieldMapper.selectByExample(field);
    }

    @Override
    public int updateByPrimaryKeySelective(Field field) {
        return fieldMapper.updateByPrimaryKeySelective(field);
    }

    @Override
    public int insertSelective(Field field) {
        return fieldMapper.insertSelective(field);
    }

    @Override
    public int deleteByPrimaryKey(Integer fieldId) {
        return fieldMapper.deleteByPrimaryKey(fieldId);
    }
    //根据代理商获取场地收益列表
    @Override
    public Msg getFieldListByUserId(Map<String, Object> paramMap,Integer userId,Integer[] integers) {
        //返回的键值对fieldVoMap
        Map<Integer, FieldVo> fieldVoMap = new HashedMap();
        if(ToolUtil.isEmpty(paramMap.get("startTime")) || ToolUtil.isEmpty(paramMap.get("endTime"))){
            return Msg.fail().add("msg", "起始时间不能为空");
        }
        if(ToolUtil.isEmpty(paramMap.get("fieldList"))){
            return Msg.fail().add("msg", "参数值错误");
        }
        //开始时间
        String startTime = (String) paramMap.get("startTime");
        //结束时间
        String endTime = (String) paramMap.get("endTime");
        //代理商id
        //Integer userId = (Integer) paramMap.get("userId");
        //场地id数组
        List<Integer> fieldList = (List<Integer>) paramMap.get("fieldList");
        if(fieldList.get(0) == 0){
           /* Field field = new Field();
            field.setFieldUserId(userId);
            List<Field> list = fieldMapper.selectByExample(field);
            fieldList.clear();
            for(Field f:list){
                fieldList.add(f.getFieldId());
            }*/
            fieldList.clear();
            if(ToolUtil.isNotEmpty(integers)){
                fieldList = Arrays.asList(integers);
                if(fieldList.size() > 0){
                    for(int i = 0;i < fieldList.size();i++){
                        //先插入对应的场地id的Vo
                        FieldVo fieldVo = new FieldVo();
                        //场地id
                        fieldVo.setFieldId(fieldList.get(i));
                        //插入到返回的map中
                        fieldVoMap.put(fieldList.get(i), fieldVo);
                    }
                }else{
                    fieldList = null;
                }
            }else {
                fieldList = null;
            }
        }

        List<Map<String, Object>> fieldListExample = fieldMapper.selectByFieldList(fieldList,userId);
        //根据场地id和时间查询onlineOrder总数{fieldId场地id，fieldName场地名称，fieldAddress场地地址，onlineOrderAmount线上总金额}
        List<Map<String, Object>> onlineOrderResultList = onlineOrderMapper.selectOnlineOrderAmountByFieldId(fieldList, startTime, endTime,userId);
        // System.out.println("线上总收益："+ onlineOrderResultList.get(0).get("onlineOrderAmount"));
        //退款
        List<Map<String,Object>> refundMap = orderBuyMapper.selectRefundByFieldId(fieldList,startTime,endTime,userId);
        //根据场地id和时间查询消耗总数 {商品总数goodsConsumeCount，fieldId场地id}
        List<Map<String, Object>> goodsCousumeResultList = goodsConsumeMapper.getGoodsAmountByFieldId(fieldList, startTime, endTime,userId);
        //根据场地id和时间查询gift礼品总价{商品总价goodsAmount,fieldId场地id}
        /* List<Map<String,Object>> goodsResultList = goodsMapper.getGoodByFieldId(fieldList, startTime, endTime);*/
        //根据场地id和时间查询场地下所有设备的总投币记录数{场地id ，线下总金额underLineAmount}
        List<Map<String,Object>> underLineOrderResultList = onlineOrderMapper.selectUnderOrderByField(fieldList,startTime,endTime,userId);
        //根据场地id查询场地下的设备总数{fieldId 场地id equipmentNumber设备总数}
        List<Map<String, Object>> equipmentResultList = equipmentMapper.getEquipmentNumberByFieldId(fieldList,userId);
        //循环遍历场地
        for(Map<String, Object> map : fieldListExample){
            FieldVo fieldVo;
            if(ToolUtil.isNotEmpty(map) && ToolUtil.isNotEmpty(map.get("fieldId")) && ToolUtil.isNotEmpty(fieldVoMap.get(map.get("fieldId")))){
                fieldVo = fieldVoMap.get(map.get("fieldId"));
            }else{
                fieldVo = new FieldVo();
            }
             //场地名称
            fieldVo.setFieldName((String) map.get("fieldName"));
            //场地地区
            fieldVo.setFieldAddress((String) map.get("fieldAddress"));
            //放回返回的键值对map中
            fieldVoMap.put((Integer) map.get("fieldId"), fieldVo);
        }
        //循环遍历equipmentResultList
        for(Map<String, Object> map:equipmentResultList){
            FieldVo fieldVo;
            if(ToolUtil.isNotEmpty(map) && ToolUtil.isNotEmpty(map.get("fieldId")) && ToolUtil.isNotEmpty(fieldVoMap.get(map.get("fieldId")))){
                fieldVo = fieldVoMap.get(map.get("fieldId"));
            }else{
                fieldVo = new FieldVo();
            }
            //设备总数
            fieldVo.setEquipmentNumber(Integer.valueOf(String.valueOf((long) map.get("equipmentNumber"))));
            //放回返回的键值对map中
            fieldVoMap.put((Integer) map.get("fieldId"), fieldVo);
        }
        //循环遍历underLineOrderResultList
        underLineOrderResultList.removeAll(Collections.singleton(null));
        if(underLineOrderResultList.size() > 0){
            for(Map<String, Object> map:underLineOrderResultList){
                FieldVo fieldVo;
                if(ToolUtil.isNotEmpty(map) && ToolUtil.isNotEmpty(map.get("fieldId")) && ToolUtil.isNotEmpty(fieldVoMap.get(map.get("fieldId")))){
                    fieldVo = fieldVoMap.get(map.get("fieldId"));
                }else{
                    fieldVo = new FieldVo();
                }
                //线下投币总数
                fieldVo.setUnderOrderAmount(Double.parseDouble(map.get("underLineAmount").toString()));
                //放回返回的键值对map中
                fieldVoMap.put((Integer) map.get("fieldId"), fieldVo);
            }
        }
        //循环遍历onlineOrderResultList
        for(Map<String,Object> map:onlineOrderResultList){
            FieldVo fieldVo;
            if(ToolUtil.isNotEmpty(map) && ToolUtil.isNotEmpty(map.get("fieldId")) && ToolUtil.isNotEmpty(fieldVoMap.get(map.get("fieldId")))){
                fieldVo = fieldVoMap.get(map.get("fieldId"));
            }else{
                fieldVo = new FieldVo();
            }
                    //线上投币总数
            fieldVo.setOnlineOrderAmount(Double.parseDouble(map.get("onlineOrderAmount").toString()));
            fieldVo.setEarnings(Double.parseDouble(map.get("onlineOrderAmount").toString()));
            //放回返回的键值对map中
            fieldVoMap.put((Integer) map.get("fieldId"), fieldVo);
        }

        for(Map<String,Object> map:refundMap){
            FieldVo fieldVo;
            if(ToolUtil.isNotEmpty(map) && ToolUtil.isNotEmpty(map.get("fieldId")) && ToolUtil.isNotEmpty(fieldVoMap.get(map.get("fieldId")))){
                fieldVo = fieldVoMap.get(map.get("fieldId"));
            }else{
                fieldVo = new FieldVo();
            }
            if(fieldVo.getFieldId() == map.get("fieldId")){
                fieldVo.setOnlineOrderAmount(BigDecimalUtils.sub(fieldVo.getOnlineOrderAmount(),
                        Double.parseDouble(map.get("moneyFile").toString())));
            }
        }

        //遍历goodsCousumeResultList
        for(Map<String,Object> map:goodsCousumeResultList){
            FieldVo fieldVo;
            if(ToolUtil.isNotEmpty(map) && ToolUtil.isNotEmpty(map.get("fieldId")) && ToolUtil.isNotEmpty(fieldVoMap.get(map.get("fieldId")))){
                fieldVo = fieldVoMap.get(map.get("fieldId"));
            }else{
                fieldVo = new FieldVo();
            }
            //礼品消耗总数
            fieldVo.setGoodsConsumeAmount(Integer.parseInt(map.get("goodsConsumeCount").toString()));
            //礼品消耗总价
            fieldVo.setGoodsAmount(Double.parseDouble( map.get("goodsAmount").toString()));
            //放回返回的键值对map中
            fieldVoMap.put((Integer) map.get("fieldId"), fieldVo);
        }
        //循环遍历返回的map
        for(Integer key:fieldVoMap.keySet()){
            //场地视图对象
            FieldVo fieldVo = fieldVoMap.get(key);
            //防止空指针异常
            if (null == fieldVo.getUnderOrderAmount()) {
                fieldVo.setUnderOrderAmount(0D);
            }
            if (null == fieldVo.getOnlineOrderAmount()) {
                fieldVo.setOnlineOrderAmount(0D);
            }
            if(null == fieldVo.getGoodsAmount()){
                fieldVo.setGoodsAmount(0D);
            }
            if(null == fieldVo.getGoodsConsumeAmount()){
                fieldVo.setGoodsConsumeAmount(0);
            }
            if(null == fieldVo.getEquipmentNumber()){
                fieldVo.setEquipmentNumber(0);
            }
            if(null == fieldVo.getEarnings()){
                fieldVo.setEarnings(0D);
            }
            //合计收益 = 线上收益 + 线下收益
            fieldVo.setAmount(fieldVo.getEarnings() + fieldVo.getUnderOrderAmount());
            //放回返回的键值对map中
            fieldVoMap.put(key, fieldVo);
        }
        return Msg.success().add("resultMap", fieldVoMap);
    }

    @Override
    public List<Field> selectFieldByUserId(Integer userId) {
        return fieldMapper.selectFieldByUserId(userId);
    }

    @Override
    public Integer[] selectIdByUserId(Integer userId) {
        return fieldMapper.selectIdByUserId(userId);
    }

    @Override
    public List<Field> selectFieldById(Integer[] integers) {
        return fieldMapper.selectFieldById(integers);
    }

    @Override
    public List<Field> selectFieldByIdPage(Integer[] integers, Integer pageStart, Integer pageSize) {
        return fieldMapper.selectFieldByIdPage(integers,pageStart,pageSize);
    }

}
